<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
      <sticky :z-index="10" :class-name="'sub-navbar '+postForm.status">
        <el-button
          v-loading="loading"
          style="margin-left: 10px;"
          type="success"
          @click="submitForm"
        >保存</el-button>
      </sticky>
      <div class="createPost-main-container">
        <el-form-item style="margin-bottom: 40px;" prop="cateName" label-width="70px" label="标题:">
          <el-input v-model="postForm.cateName" type="text" placeholder="请输入标题" />
        </el-form-item>

        <el-form-item style="margin-bottom: 40px;" prop="desr" label-width="70px" label="说明:">
          <el-input v-model="postForm.desr" type="text" placeholder="请输入说明" />
        </el-form-item>

        <el-form-item style="margin-bottom: 40px;" prop="sort" label-width="70px" label="排序:">
          <el-input v-model.number="postForm.sort" type="text" placeholder="请输入数字" />
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import Sticky from "@/components/Sticky"; // 粘性header组件
import { createCate, updataCate, fetchOne } from "@/api/product-cate";

export default {
  name: "ProductCateDetail",
  components: { Sticky },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      postForm: {
        cateName: "",
        desr: "",
        sort: null,
        hidden: 0,
        id: null,
        pid: 0
      },
      loading: false,
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        instr: [{ required: true, message: "请输入说明", trigger: "blur" }],
        sort: [{ required: true, message: "请输入数字", trigger: "blur" }]
      },
      tempRoute: {}
    };
  },
  computed: {
    
  },
  created() {
    if (this.isEdit) {
      let id = this.$route.params && this.$route.params.id;
      this.postForm.id=id;

      fetchOne({id: id}).then(response => {
        if (response.success == true) {
          this.postForm.cateName = response.data.cateName
          this.postForm.desr = response.data.desr
          this.postForm.sort = response.data.sort

          // set tagsview title
          this.setTagsViewTitle()
          // set page title
          this.setPageTitle()
        }
      });
    }

    // Why need to make a copy of this.$route here?
    // Because if you enter this page and quickly switch tag, may be in the execution of the setTagsViewTitle function, this.$route is no longer pointing to the current page
    // https://github.com/PanJiaChen/vue-element-admin/issues/1221
    this.tempRoute = Object.assign({}, this.$route);
  },
  methods: {
    setTagsViewTitle() {
      const title = 'Edit Product Category'
      const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.postForm.id}` })
      this.$store.dispatch('tagsView/updateVisitedView', route)
    },
    setPageTitle() {
      const title = 'Edit Product'
      document.title = `${title} - ${this.postForm.id}`
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if(this.isEdit){
            // 更新
            updataCate(this.postForm).then(response => {
              console.log(response.errorDetail);
              if (response.success == true) {
                this.$notify({
                  title: "修改成功",
                  message: "修改分类成功",
                  type: "success",
                  duration: 2000
                });
                this.$router.go(-1);
              } else {
                this.$notify({
                  title: "失败",
                  message: response.errorDetail,
                  type: "error",
                  duration: 2000
                });
              }
            })
          }else{
            // 创建
            createCate(this.postForm).then(response => {
              if (response.success == true) {
                this.$notify({
                  title: "成功",
                  message: "添加分类成功",
                  type: "success",
                  duration: 2000
                });
                this.$router.go(-1);
              } else {
                this.$notify({
                  title: "失败",
                  message: response.errorDetail,
                  type: "error",
                  duration: 2000
                });
              }
            });
          }
         

          this.loading = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.createPost-container {
  position: relative;

  .createPost-main-container {
    padding: 40px 45px 20px 50px;

    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;

      .postInfo-container-item {
        float: left;
      }
    }
  }

  .word-counter {
    width: 40px;
    position: absolute;
    right: 10px;
    top: 0px;
  }
}

.article-textarea ::v-deep {
  textarea {
    padding-right: 40px;
    resize: none;
    border: none;
    border-radius: 0px;
    border-bottom: 1px solid #bfcbd9;
  }
}
</style>
